Algebraic Subtyping

نویسندگان

  • Stephen Dolan
  • Daan Leijen
چکیده

data types can be expressed neatly by treating their definition as transparent inside the module defining them, and as opaque outside it. Transparent definitions are easily implemented. In principle, they can be expanded in a preprocessing phase and ignored afterwards. In practice, this may cause an exponential increase in compile time as their definitions may be much larger than their names. This issue, however, is exactly the same as that faced by implementations of, say, Standard ML or Haskell. The addition of subtyping does not make this any more difficult, and the standard solutions (lazy expansion of type aliases) apply. So, we do not discuss this issue further. Opaque type definition cannot be preprocessed away. Instead, for each opaque type definition, we introduce a new component to the definition of the type lattice. For type definitions without parameters (like id above), this new component is simply 1, giving such definitions the same status as type variables or the type bool. Types with parameters are not much more difficult. To define a parameterised type ListPair[S, T], representing immutable lists whose elements are pairs of S and T, we simply add a component A×A to the definition of types. In general, the component corresponding to a type definition is a finite product of A and Aop, depending on whether the parameters are coor contra-variant. (In the example ListPair[S, T], both parameters are covariant). 9.1.1 Variance and mutability Most languages which support subtyping and user-defined types, from objectoriented languages like Java to functional ones like OCaml, support invariant parameters as well as coand contra-variant ones. Invariant parameters are neither conor contra-variant. If a type ty[T] is invariant, then ty[X] is a subtype of ty[Y] only when X and Y are equal. The classical examples of invariant parameters involve mutability. Suppose we have a type MList[T] of mutable lists whose elements are of type T, equipped with operations get and put to read and write elements of the list. The type of get applied such a list mentions T only in its return type, and is therefore covariant in T. However, the type of put applied to such a list mentions T only in its argument type, and is contravariant in T. The type MList[T] cannot therefore be said to be either coor contra-variant in T. Unlike coand contra-variant parameters, invariant parameters cannot be encoded using the small set of ingredients we have allowed ourselves. This is not a simple omission: many parts of the system described in this thesis assume that all fields of all type constructors are either coor contra-variant. For instance, the biunify algorithm produces a bisubstitution, acting differently on positive and negative occurrences of variables, while implicitly assuming that all occurrences of a variable are one or the other. Rather than admit defeat here, I argue that invariance is a poor way to describe the parameter of MList, which makes it gratuitously difficult to typecheck quite reasonable pieces of code. Consider the following piece of code in Java (where ArrayList plays the role of MList[T]): void disableAll(ArrayList comps) { for (Component c : comps) { c.setEnabled(false); } CHAPTER 9. EXTENSIONS 131

برای دانلود متن کامل این مقاله و بیش از 32 میلیون مقاله دیگر ابتدا ثبت نام کنید

ثبت نام

اگر عضو سایت هستید لطفا وارد حساب کاربری خود شوید

منابع مشابه

GADTs Meet Subtyping

While generalized algebraic datatypes (GADTs) are now considered well-understood, adding them to a language with a notion of subtyping comes with a few surprises. What does it mean for a GADT parameter to be covariant? The answer turns out to be quite subtle. It involves fine-grained properties of the subtyping relation that raise interesting design questions. We allow variance annotations in G...

متن کامل

Explicit Effect Subtyping (Extended Version)

As popularity of algebraic effects and handlers increases, so does a demand for their efficient execution. Eff, an ML-like language with native support for handlers, has a subtyping-based effect system on which an effect-aware optimizing compiler could be built. Unfortunately, in our experience, implementing optimizations for Eff is overly error-prone because its core language is implicitly-typ...

متن کامل

Subtyping and Inheritance for Categorical

We extend Hagino's categorical datatypes with subtyping and a limited form of inheritance. The view of objects as coalgebras provides the inspiration for subtyping and inheritance for coalgebraic (or coinductive) types. Exploiting the duality between coalgebras and algebras then yields notions of subtyping and inheritance for algebraic (or inductive) types.

متن کامل

An Object Oriented Speci cation Language based on Hierarchical Algebraic Petri Nets

During the last few years there has been signiicant interest in combining the beneets of an object-oriented approach with Petri net formalism which is suitable to modelling and simulation of concurrent systems. Nevertheless, for modelling some system behaviour dynamic creation of new components is often needed. In this paper we introduce a formalism based on algebraic Petri nets using the objec...

متن کامل

GADT meet Subtyping

While generalized abstract datatypes (GADT) are now considered well-understood, adding them to a language with a notion of subtyping comes with a few surprises. What does it mean for a GADT parameter to be covariant? The answer turns out to be quite subtle. It involves fine-grained properties of the subtyping relation that raise interesting design questions. We allow variance annotations in GAD...

متن کامل

Phantom Types and Subtyping

We investigate a technique from the literature, called the phantom-types technique, that uses parametric polymorphism, type constraints, and unification of polymorphic types to model a subtyping hierarchy. Hindley-Milner type systems, such as the one found in Standard ML, can be used to enforce the subtyping relation, at least for first-order values. We show that this technique can be used to e...

متن کامل

ذخیره در منابع من


  با ذخیره ی این منبع در منابع من، دسترسی به آن را برای استفاده های بعدی آسان تر کنید

برای دانلود متن کامل این مقاله و بیش از 32 میلیون مقاله دیگر ابتدا ثبت نام کنید

ثبت نام

اگر عضو سایت هستید لطفا وارد حساب کاربری خود شوید

عنوان ژورنال:

دوره   شماره 

صفحات  -

تاریخ انتشار 2016